package gebruikersinterface;

/**
* @author Jelle Maes
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import java.lang.Thread;
import java.util.ResourceBundle;

import domein.DomeinController;

@SuppressWarnings("serial")
public class NieuweRekening extends JFrame{
	private JButton cancelKnop,okKnop,rekeningtoewijzenKnop;
	private JTextField opzoekenklant;	
	private JScrollPane pane;
	private Scherm scherm;
	private String[][] klantenArray;
	private JTable klantenLijst;
	private ResourceBundle strings;
	
	public NieuweRekening(Scherm scherm) {
		super();
		this.scherm = scherm;
		this.refresh("");	
	}
	
	public void refresh(String s){
		this.dispose();
		if(!s.equals("")){
			this.remove(this.klantenLijst);
			this.remove(this.pane);
			this.remove(this.cancelKnop);
			this.remove(this.okKnop);
			this.remove(this.rekeningtoewijzenKnop);
			this.remove(this.opzoekenklant);
		}

		this.setLayout(null);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setUndecorated(false);
		this.setSize(500,400);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		
		this.opzoekenklant = new JTextField(s);
			this.opzoekenklant.setBounds(30,20,400,20);
			this.opzoekenklant.addActionListener( new ActionListener() {
				public void actionPerformed(ActionEvent event){
					refresh(opzoekenklant.getText());
				}
			});
			this.add(this.opzoekenklant);
			
		this.rekeningtoewijzenKnop = new JButton();
			this.rekeningtoewijzenKnop.setBounds(30,200,100,40);
			this.rekeningtoewijzenKnop.setEnabled(false);
			this.add(this.rekeningtoewijzenKnop);  
			
		this.okKnop= new JButton();
			this.okKnop.setBounds(150,200,100,40);
			this.add(this.okKnop);  
			
		this.cancelKnop = new JButton();
			this.cancelKnop.setBounds(270,200,100,40);
			this.add(this.cancelKnop);
		
		this.vulLabels();
			
		this.klantenArray = DomeinController.getInstance().getKlantenLijst(s);
			String[] headers = {this.strings.getString("naamLabel"),this.strings.getString("voornaamLabel")};
			this.klantenLijst = null;
			this.klantenLijst = new JTable(this.klantenArray, headers);
			
		this.pane = null;
		this.pane = new JScrollPane(this.klantenLijst);
			this.pane.setBorder(null);
			this.pane.setBounds(30,50,400,100);
			this.add(this.pane);

		this.cancelKnop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event){
				dispose();
			}
		});
			
		this.okKnop.addActionListener(new ActionListener() {
			@SuppressWarnings("static-access")
			public void actionPerformed(ActionEvent event){
				selecteerKlant();
			}
		});

		this.rekeningtoewijzenKnop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event){
			}
		});	
		
		this.repaint();
		this.setVisible(true);
	}
					
		private void vulLabels() {
			this.strings = Taal.getBundle("nieuwBundle");
			this.setTitle("FloorMaster || "+ this.strings.getString("titelLabel"));
			this.rekeningtoewijzenKnop.setText(this.strings.getString("nieuweKlantLabel"));
			this.cancelKnop.setText(this.strings.getString("annuleerLabel"));
			this.okKnop.setText(this.strings.getString("selecteerLabel"));	
		}
		
		@SuppressWarnings("static-access")
		public void selecteerKlant(){
			try{
				DomeinController.getInstance().setHuidigeKlant(Integer.parseInt(klantenArray[klantenLijst.getSelectedRow()][2]));
				DomeinController.getInstance().addRekening();
				Thread.currentThread().sleep(500); //anders is de databank niet mee... hoe het komt.. Joost mag het weten...
				scherm.refresh(true);
				dispose();
				}
				catch(ArrayIndexOutOfBoundsException e){
					JOptionPane.showMessageDialog(null,"Gelieve een klant te selecteren!","Selecteer een klant",JOptionPane.ERROR_MESSAGE);
				}
				catch(InterruptedException e){
					
				}
		}
}
				
